<template>
  <div class="breadcrumb-navigation" v-if="breadcrumbList.length > 0">
    <el-breadcrumb separator="/" class="breadcrumb">
      <el-breadcrumb-item
        v-for="item in breadcrumbList"
        :key="item.path"
        :to="item.path"
      >
        <el-icon v-if="item.icon" class="breadcrumb-icon">
          <component :is="item.icon" />
        </el-icon>
        <span>{{ item.title }}</span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    
    <!-- 页面操作按钮 -->
    <div class="page-actions" v-if="showActions">
      <slot name="actions">
        <!-- 默认操作按钮 -->
        <el-button
          v-if="showRefresh"
          :icon="Refresh"
          circle
          size="small"
          @click="handleRefresh"
          title="刷新页面"
        />
        <el-button
          v-if="showBack"
          :icon="ArrowLeft"
          circle
          size="small"
          @click="handleBack"
          title="返回上级"
        />
      </slot>
    </div>
  </div>
</template>

<script setup>
/* eslint-disable no-undef */
import { computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Refresh, ArrowLeft, User } from '@element-plus/icons-vue'

// Props
defineProps({
  showActions: {
    type: Boolean,
    default: true
  },
  showRefresh: {
    type: Boolean,
    default: true
  },
  showBack: {
    type: Boolean,
    default: true
  }
})

const route = useRoute()
const router = useRouter()

// 路由图标映射
const routeIconMap = {
  '/dashboard': 'Odometer',
  '/data': 'FolderOpened',
  '/data/import': 'Upload',
  '/users/manage': 'User',
  '/analysis': 'TrendCharts',
  '/analysis/profile': 'User',
  '/analysis/profile/basic': 'User',
  '/analysis/profile/behavior': 'Monitor',
  '/analysis/profile/consumption': 'Money',
  '/analysis/segmentation': 'Grid',
  '/prediction': 'MagicStick',
  '/prediction/churn': 'Warning',
  '/prediction/purchase': 'ShoppingCart',
  '/recommendation': 'Star',
  '/datav': 'Monitor',
  '/datav/monitor': 'View',
  '/datav/user-analysis': 'UserFilled',
  '/datav/sales-analysis': 'TrendCharts',
  '/datav/operation': 'Setting',
  '/models': 'MagicStick',
  '/models/browser': 'View',
  '/models/train': 'VideoPlay',
  '/models/evaluate': 'DataAnalysis',
  '/system': 'Tools',
  '/system/pages': 'Connection',
  '/system/monitor': 'Monitor'
}

// 路由标题映射
const routeTitleMap = {
  '/dashboard': '概览仪表板',
  '/users/manage': '用户管理',
  '/analysis': '数据分析',
  '/analysis/profile': '用户画像分析',
  '/analysis/profile/basic': '基础画像',
  '/analysis/profile/behavior': '行为画像',
  '/analysis/profile/consumption': '消费画像',
  '/analysis/segmentation': '用户分群',
  '/prediction': '预测分析',
  '/prediction/churn': '流失预测',
  '/prediction/purchase': '购买预测',
  '/recommendation': '推荐系统',
  '/datav': '可视化大屏',
  '/datav/monitor': '实时监控大屏',
  '/datav/user-analysis': '用户分析大屏',
  '/datav/sales-analysis': '销售分析大屏',
  '/datav/operation': '运营决策大屏',
  '/reports': '报表系统',
  '/reports/standard': '标准报表',
  '/reports/custom': '自定义报表',
  '/system': '系统管理',
  '/system/pages': '页面结构管理',
  '/system/monitor': '系统监控'
}

// 生成面包屑列表
const breadcrumbList = computed(() => {
  const pathArray = route.path.split('/').filter(path => path)
  const breadcrumbs = []
  
  // 添加首页
  if (route.path !== '/dashboard') {
    breadcrumbs.push({
      path: '/dashboard',
      title: '首页',
      icon: 'HomeFilled'
    })
  }
  
  // 构建路径面包屑
  let currentPath = ''
  pathArray.forEach((path) => {
    currentPath += `/${path}`
    
    // 跳过一些中间路径
    if (shouldSkipPath(currentPath)) {
      return
    }
    
    const title = routeTitleMap[currentPath] || formatPathTitle(path)
    const icon = routeIconMap[currentPath]
    
    breadcrumbs.push({
      path: currentPath,
      title,
      icon
    })
  })
  
  return breadcrumbs
})

// 判断是否跳过某些路径
const shouldSkipPath = (path) => {
  // 跳过中间的profile路径，直接显示具体页面
  return path === '/analysis/profile'
}

// 格式化路径标题
const formatPathTitle = (path) => {
  // 将路径转换为标题格式
  return path.charAt(0).toUpperCase() + path.slice(1).replace(/-/g, ' ')
}

// 刷新页面
const handleRefresh = () => {
  window.location.reload()
}

// 返回上级
const handleBack = () => {
  if (breadcrumbList.value.length > 1) {
    const parentPath = breadcrumbList.value[breadcrumbList.value.length - 2].path
    router.push(parentPath)
  } else {
    router.push('/dashboard')
  }
}

// 监听路由变化，更新页面标题
watch(
  () => route.path,
  () => {
    const currentBreadcrumb = breadcrumbList.value[breadcrumbList.value.length - 1]
    if (currentBreadcrumb) {
      document.title = `${currentBreadcrumb.title} - 电商用户行为分析系统`
    }
  },
  { immediate: true }
)
</script>

<style scoped>
.breadcrumb-navigation {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background-color: #ffffff;
  border-bottom: 1px solid #e6e8eb;
  min-height: 56px;
}

.breadcrumb {
  flex: 1;
}

.breadcrumb :deep(.el-breadcrumb__item) {
  font-size: 14px;
}

.breadcrumb :deep(.el-breadcrumb__item:not(.is-link)) {
  color: #333;
  font-weight: 500;
}

.breadcrumb :deep(.el-breadcrumb__item.is-link) {
  color: #666;
  transition: color 0.3s ease;
}

.breadcrumb :deep(.el-breadcrumb__item.is-link:hover) {
  color: #2a5298;
}

.breadcrumb :deep(.el-breadcrumb__separator) {
  color: #c0c4cc;
  margin: 0 8px;
}

.breadcrumb-icon {
  margin-right: 4px;
  font-size: 14px;
  vertical-align: middle;
}

.page-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-actions :deep(.el-button) {
  border: 1px solid #e6e8eb;
  background-color: #ffffff;
  color: #666;
  transition: all 0.3s ease;
}

.page-actions :deep(.el-button:hover) {
  border-color: #2a5298;
  color: #2a5298;
  background-color: #f0f4ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .breadcrumb-navigation {
    padding: 12px 16px;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .page-actions {
    align-self: flex-end;
  }
  
  .breadcrumb :deep(.el-breadcrumb__item) {
    font-size: 13px;
  }
  
  .breadcrumb-icon {
    display: none;
  }
}

@media (max-width: 480px) {
  .breadcrumb-navigation {
    padding: 8px 12px;
  }
  
  .breadcrumb :deep(.el-breadcrumb__separator) {
    margin: 0 4px;
  }
  
  .page-actions {
    gap: 4px;
  }
}
</style>
